class Solution {
public:
    int minNumberOfFrogs(string croakOfFrogs) {
        int c = 0, r = 0, o = 0, a = 0, k = 0;
        if (croakOfFrogs[0] == 'c') c++;
        else return -1;
        for (int i = 1; i < croakOfFrogs.size(); i++) {
            if (croakOfFrogs[i] == 'c' && k > 0) { c++, k--; }
            else if (croakOfFrogs[i] == 'c' && k == 0) { c++; }
            else if (croakOfFrogs[i] == 'r' && c > 0) { c--, r++; }
            else if (croakOfFrogs[i] == 'o' && r > 0) { r--, o++; }
            else if (croakOfFrogs[i] == 'a' && o > 0) { o--, a++; }
            else if (croakOfFrogs[i] == 'k' && a > 0) { a--, k++; }
            else return -1;
        }
        if (c == 0 && r == 0 && o == 0 && a == 0) return k;
        else return -1;
    }
};